import request from '../utils/request'

// 收藏相关接口
export const favoriteApi = {
    // 检查收藏状态
    checkFavorite: (userid, targetid, type) => {
        return request.get('/favorites/check', { userid, targetid, type })
    },
    // 添加收藏
    addFavorite: (userid, targetid, type) => {
        return request.get('/favorites/add', { userid, targetid, type })
    },
    // 取消收藏
    deleteFavorite: (userid, targetid, type) => {
        return request.get('/favorites/delete', { userid, targetid, type })
    },    // 获取收藏列表
    getFavorites: (userid) => {
        return request.get('/favorites/list', { userid })
    }
}

// 营地相关接口
export const campApi = {    // 获取营地列表
    getCampList: (params = {}) => {
        // 确保参数类型正确并过滤掉无效值
        const queryParams = {};
        if (params.categoryId) {
            queryParams.categoryId = parseInt(params.categoryId);
        }
        if (params.cityId) {
            queryParams.cityId = parseInt(params.cityId);
        }
        if (params.keyword) {
            queryParams.name = params.keyword;
        }
        return request.get('/camplist', queryParams);
    },
    // 获取营地分类
    getCategories: () => {
        return request.get('/categories')
    },
    // 获取轮播图 
    getBannerList: () => {
        return request.get('/camp/banner')
    }
}

// 攻略相关接口
export const guideApi = {
    // 获取攻略列表
    getGuideList: () => {
        return request.get('/guides')
    },
    // 获取攻略详情
    getGuideDetail: (id) => {
        return request.get(`/guide/detail/${id}`)
    },
    // 获取攻略分类
    getGuideCategories: () => {
        return request.get('/guides/categories')
    }
}

// 用户相关接口
export const userApi = {    // 微信登录
    login: ({ code, ...userData }) => {
        return request.post(`/user/login/wechat/${code}`, userData)
    },
    // 更新用户信息
    updateUser: (data) => {
        return request.put('/user/update', data)
    },
    // 上传头像
    uploadAvatar: (filePath) => {
        return request.upload('/file/upload', filePath)
    }
}

// 订单相关接口
export const orderApi = {
    // 创建订单
    createOrder: (data) => {
        return request.post('/booking/create', data)
    },
    // 获取订单列表
    getOrderList: (userId) => {
        return request.get('/booking/list', { userId })
    },
    // 修改订单状态
    updateBookingStatus: (data) => {
        return request.post('/booking/update', data)
    },
    // 支付订单
    handlePayment: (campId, userId) => {
        return orderApi.updateBookingStatus({
            campId,
            userId,
            status: 'paid'  // 更新为已支付状态
        })
    },
    // 取消订单
    cancelOrder: (campId, userId) => {
        return orderApi.updateBookingStatus({
            campId,
            userId,
            status: 'cancelled'  // 更新为已取消状态
        })
    },
    // 完成订单
    completeOrder: (campId, userId) => {
        return orderApi.updateBookingStatus({
            campId,
            userId,
            status: 'completed'  // 更新为已完成状态
        })
    }
}

// 城市相关接口
export const cityApi = {
    // 获取城市列表
    getCityList: () => {
        return request.get('/cities')
    }
}
